package kr.hyunist.androidcomplete02saeildamil.ch33;

import java.util.ArrayList;

import kr.hyunist.androidcomplete02saeildamil.R;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	private ArrayList<MyMusic> arList;
	private Context mContext;
	LayoutInflater mInflater;
	
	public ListViewAdapter(Context conctext, ArrayList<MyMusic> mList) {

		mContext = conctext;
		arList = mList;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		getAllSongsInfo();
		
	}

	public int getCount() {
		return arList.size();
	}

	public Object getItem(int position) {
		return arList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.song_item, parent, false);
		}

		TextView singer = (TextView) convertView.findViewById(R.id.singer);
		TextView title = (TextView) convertView.findViewById(R.id.title);
		TextView time = (TextView) convertView.findViewById(R.id.playTime);
		ImageView albumImg = (ImageView) convertView.findViewById(R.id.albumart);
		
		Bitmap albumArt = Util.getArtworkQuick(mContext, Integer.parseInt((arList.get(position).getAlbumID())), 50, 50);
		if(albumArt == null){
			albumImg.setImageResource(R.drawable.noimage);
		} else {
			albumImg.setImageBitmap(albumArt);
		}
		
		singer.setText(arList.get(position).getArtist());
		title.setText(arList.get(position).getMusicTitle());
		title.setSelected(true);
		time.setText(arList.get(position).getPlayTime());

		return convertView;

	}

	//기기 내에 있는 모든 음악을 검색하되  mp3만 검색
	public void getAllSongsInfo() {
		String[] proj = { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION,
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.MIME_TYPE };

		// mime_type을 audio/mpeg으로 가져온다. null이면 wma파일까지 모두 가져온다.
		ContentResolver cr = mContext.getContentResolver();
		Cursor musicCursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, "mime_type = '" + "audio/mpeg'", null, null);
		
		if (musicCursor != null && musicCursor.moveToFirst()) {

			arList.clear();

			int musicIDCol = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
			int albumIDCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
			int musicTitleCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
			int singerCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
			int dataColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
			int playTimeCol = musicCursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
						
			String musicID;
			String albumID;
			String musicTitle;
			String artist;
			String playTime;
			String data;
			
			while (musicCursor.moveToNext()) {
				musicID = musicCursor.getString(musicIDCol);
				albumID = musicCursor.getString(albumIDCol);
				musicTitle = musicCursor.getString(musicTitleCol);
				artist = musicCursor.getString(singerCol);
				data = musicCursor.getString(dataColumn);
				playTime = musicCursor.getString(playTimeCol);

				MyMusic mySong = new MyMusic(musicID, albumID, musicTitle, artist, data, Util.milliSecToTime(playTime));
				arList.add(mySong);
			}

		}
		
		musicCursor.close();
		return;
	}
	
	//기기내에 음악을 폴더 단위로 검색
	public void getFolderSongInfo(){
		
	}
}
